package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@Slf4j
@EnableScheduling
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 凡是涉及到数据库变更数据的接口 都要删除缓存内的数据 保持缓存与数据库内存的一致性
     * 只有用户端查询时，才插入缓存
     */

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐接口")
    //清理缓存                   只要数据改变 就要清理缓存 保持缓存与数据库数据一致性
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//setmealCache::#setmealDTO.categoryId
    public Result AddSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.AddSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐接口")
    public Result<PageResult> PageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setmealService.PageSetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除/批量删除套餐的
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除/批量删除套餐接口")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true,beforeInvocation = true)
    public Result DeleteSetmeal(@RequestParam List<Long> ids){
        setmealService.DeleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐接口")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true,beforeInvocation = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.UpdateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 根据ID查询套餐 用于回显数据 修改套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询套餐")
    public Result<SetmealVO> SelectByIDSetmeal(@PathVariable Long id){
        SetmealVO setmealVO=setmealService.SelectByIDSetmeal(id);
        return Result.success(setmealVO);
    }

    /**
     * 套餐的起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐的启售/停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true,beforeInvocation = true)
    public Result StartORStop(@PathVariable String status,Long id){
        setmealService.StartORStop(status,id);
        return Result.success();
    }


}
